home *** CD-ROM | disk | FTP | other *** search
- {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- (c) TechInsite Pty. Ltd.
- PO Box 429, Abbotsford, Melbourne. 3067 Australia
- Phone: +61 3 9419 6456
- Fax: +61 3 9419 1682
- Web: www.techinsite.com.au
- EMail: peter_hinrichsen@techinsite.com.au
-
- Created: Jan 2000
-
- Notes: Family of visitors to manage relational database persistence.
-
- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
- unit tiPtnVisitorDB;
-
- interface
- uses
- tiPtnVisitor
- ,tiDBConnection
- ,DBTables
- ;
-
- type
-
- // TVisDBAbs: Abstract base database visitor
- //----------------------------------------------------------------------------
- TVisDBAbs = class( TVisitorAbs )
- private
- FQuery : TQuery ;
- FDBConnection : TtiDBConnection;
- procedure SetDBConnection(const Value: TtiDBConnection);
- protected
- function AcceptVisitor : boolean ; override ;
- procedure Init ; virtual ;
- procedure SetupParams ; virtual ;
- procedure MapRowsToObject ; virtual ;
- procedure UpdateObject ; virtual ;
- property Query : TQuery read FQuery ;
- public
- constructor Create ; override ;
- destructor Destroy ; override ;
- property DBConnection : TtiDBConnection read FDBConnection write SetDBConnection ;
- end ;
-
- // TVisDBSelect: Abstract base visitor for mapping the results of a SELECT
- // statement to a list of objects.
- //----------------------------------------------------------------------------
- TVisDBSelect = class( TVisDBAbs )
- private
- public
- procedure Execute( pVisited : TVisitedAbs ) ; override ;
- end ;
-
- // TVisDBUpdate: Abstract base visitor for mapping an object to a SQL
- // CREATE, UPDATE or DELETE statement
- //----------------------------------------------------------------------------
- TVisDBUpdate = class( TVisDBAbs )
- private
- public
- procedure Execute( pVisited : TVisitedAbs ) ; override ;
- end ;
-
-
- implementation
-
- // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- // *
- // * TVisDBAbs
- // *
- // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- constructor TVisDBAbs.Create;
- begin
- inherited Create ;
- FQuery := TQuery.Create( nil ) ;
- end;
-
- //------------------------------------------------------------------------------
- destructor TVisDBAbs.Destroy;
- begin
- FQuery.Free ;
- inherited;
- end;
-
- //------------------------------------------------------------------------------
- function TVisDBAbs.AcceptVisitor : boolean;
- begin
- // Implement in the concrete.
- result := false ;
- end;
-
- //------------------------------------------------------------------------------
- procedure TVisDBAbs.Init;
- begin
- // Do nothing, implement in the concrete
- end;
-
- //------------------------------------------------------------------------------
- procedure TVisDBAbs.SetupParams;
- begin
- // Do nothing, implement in the concrete
- end;
-
- //------------------------------------------------------------------------------
- procedure TVisDBAbs.MapRowsToObject;
- begin
- // Do nothing, implement in the concrete
- end;
-
- //------------------------------------------------------------------------------
- procedure TVisDBAbs.UpdateObject;
- begin
- // Do nothing, implement in the concrete
- end;
-
- //------------------------------------------------------------------------------
- procedure TVisDBAbs.SetDBConnection(const Value: TtiDBConnection);
- begin
- FDBConnection := Value;
- if Value <> nil then
- FQuery.DatabaseName := FDBConnection.DatabaseName
- else begin
- if FQuery.Active then
- FQuery.Close ;
- FQuery.DatabaseName := '' ;
- end ;
- end;
-
- // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- // *
- // * TVisDBSelect
- // *
- // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- procedure TVisDBSelect.Execute(pVisited: TVisitedAbs);
- begin
- inherited Execute( pVisited ) ;
- try
- if not AcceptVisitor then
- exit ; //==>
-
- Init ;
- SetupParams ;
-
- Query.Open ;
- while not Query.EOF do begin
- MapRowsToObject ;
- Query.Next ;
- end ;
-
- UpdateObject ;
-
- finally
- Visited := nil ;
- end ;
- end;
-
- // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- // *
- // * TVisDBUpdate
- // *
- // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- procedure TVisDBUpdate.Execute(pVisited: TVisitedAbs);
- begin
- inherited Execute( pVisited ) ;
- try
- if not AcceptVisitor then
- exit ; //==>
-
- Init ;
- SetupParams ;
- MapRowsToObject ;
- Query.ExecSQL ;
- UpdateObject ;
-
- finally
- Visited := nil ;
- end ;
-
- end ;
-
- end.
-